home *** CD-ROM | disk | FTP | other *** search
/ An Introduction to Progr…l Basic 6.0 (4th Edition) / An Introduction to Programming using Visual Basic 6.0.iso / PROGRAMS / CH8 / 8-2-1.FRM (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1998-09-24  |  3.6 KB  |  113 lines

  1. VERSION 5.00
  2. Begin VB.Form frm8_2_1 
  3.    Caption         =   "8-2-1"
  4.    ClientHeight    =   1704
  5.    ClientLeft      =   1092
  6.    ClientTop       =   1488
  7.    ClientWidth     =   2676
  8.    BeginProperty Font 
  9.       Name            =   "MS Sans Serif"
  10.       Size            =   7.8
  11.       Charset         =   0
  12.       Weight          =   700
  13.       Underline       =   0   'False
  14.       Italic          =   0   'False
  15.       Strikethrough   =   0   'False
  16.    EndProperty
  17.    LinkTopic       =   "Form1"
  18.    PaletteMode     =   1  'UseZOrder
  19.    ScaleHeight     =   1704
  20.    ScaleWidth      =   2676
  21.    Begin VB.PictureBox picShowData 
  22.       Height          =   855
  23.       Left            =   120
  24.       ScaleHeight     =   804
  25.       ScaleWidth      =   2364
  26.       TabIndex        =   1
  27.       Top             =   720
  28.       Width           =   2415
  29.    End
  30.    Begin VB.CommandButton cmdSort 
  31.       Caption         =   "Sort by Year of Birth"
  32.       Height          =   495
  33.       Left            =   120
  34.       TabIndex        =   0
  35.       Top             =   120
  36.       Width           =   2415
  37.    End
  38. Attribute VB_Name = "frm8_2_1"
  39. Attribute VB_GlobalNameSpace = False
  40. Attribute VB_Creatable = False
  41. Attribute VB_PredeclaredId = True
  42. Attribute VB_Exposed = False
  43. Private Sub cmdSort_Click()
  44.   Dim numPeople As Integer
  45.   'Sort data from YOB.TXT file by year of birth
  46.   numPeople = NumberOfRecords(App.Path & "\YOB.TXT")
  47.   ReDim nom(1 To numPeople) As String
  48.   ReDim yearBorn(1 To numPeople) As Integer
  49.   Call ReadData(nom(), yearBorn(), numPeople)
  50.   Call SortData(nom(), yearBorn(), numPeople)
  51.   Call ShowData(nom(), yearBorn(), numPeople)
  52.   Call WriteData(nom(), yearBorn(), numPeople)
  53. End Sub
  54. Private Function NumberOfRecords(filespec As String) As Integer
  55.   Dim nom As String, yearBorn As Integer
  56.   Dim n As Integer    'Used to count records
  57.   n = 0
  58.   Open filespec For Input As #1
  59.   Do While Not EOF(1)
  60.     Input #1, nom, yearBorn
  61.     n = n + 1
  62.   Loop
  63.   Close #1
  64.   NumberOfRecords = n
  65. End Function
  66. Private Sub ReadData(nom() As String, yearBorn() As Integer, numPeople As Integer)
  67.   Dim index As Integer
  68.   'Read data from file into arrays
  69.   Open App.Path & "\YOB.TXT" For Input As #1
  70.   For index = 1 To numPeople
  71.     Input #1, nom(index), yearBorn(index)
  72.   Next index
  73.   Close #1
  74. End Sub
  75. Private Sub ShowData(nom() As String, yearBorn() As Integer, numPeople As Integer)
  76.   Dim index As Integer
  77.   'Display the sorted list
  78.   picShowData.Cls
  79.   For index = 1 To numPeople
  80.     picShowData.Print nom(index), yearBorn(index)
  81.   Next index
  82. End Sub
  83. Private Sub SortData(nom() As String, yearBorn() As Integer, numPeople As Integer)
  84.   Dim passNum As Integer, index As Integer
  85.   'Bubble sort arrays by year of birth
  86.   For passNum = 1 To numPeople - 1
  87.     For index = 1 To numPeople - passNum
  88.       If yearBorn(index) > yearBorn(index + 1) Then
  89.           Call SwapData(nom(), yearBorn(), index)
  90.       End If
  91.     Next index
  92.   Next passNum
  93. End Sub
  94. Private Sub SwapData(nom() As String, yearBorn() As Integer, index As Integer)
  95.   Dim stemp As String, ntemp As Integer
  96.   'Swap names and years
  97.   stemp = nom(index)
  98.   nom(index) = nom(index + 1)
  99.   nom(index + 1) = stemp
  100.   ntemp = yearBorn(index)
  101.   yearBorn(index) = yearBorn(index + 1)
  102.   yearBorn(index + 1) = ntemp
  103. End Sub
  104. Private Sub WriteData(nom() As String, yearBorn() As Integer, numPeople As Integer)
  105.   Dim index As Integer
  106.   'Write data back into file
  107.   Open App.Path & "\YOB.TXT" For Output As #1
  108.   For index = 1 To numPeople
  109.     Write #1, nom(index), yearBorn(index)
  110.   Next index
  111.   Close #1
  112. End Sub
  113.